package savelog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

/**
 * Title: SettingActivity class for CSIS 343 Final Project Description: CS 343
 * Final Project - SettingActivity This class allows the users to interact with
 * the settings that our app has. It incorporates a variety of different
 * components and also saving settings and data.
 * 
 * @author Erik Olson
 * @email erik.olson-5@my.uwrf.edu
 * @author Mike Harmon
 * @email michael.harmon@my.uwrf.edu
 * @author Jack Friermuth
 * @email jack.friermuth@my.uwrf.edu
 * @author Luke Vang
 * @email luke.vang@my.uwrf.edu
 * @author Casey Golla
 * @email casey.golla@my.uwrf.edu
 * @date December 4th 2012
 */

public class SettingActivity extends Activity {

	// Declaration of variables for storing data based on
	// components of the SettingActivity.xml, such as:
	// the device name, which directory each one is directed at,
	// buttons, and a radiogroup for layout choices.

	private static String m_chosenN64Dir = "";
	private static String m_chosenSNesDir = "";
	private static String m_chosenNintendoDir = "";
	private static String m_chosenSegaDir = "";
	private static String m_chosenGBADir = "";
	private static String m_chosenPathDir = "";
	private static String deviceNamePref = "";
	private static String setting_layout = "";
	private static boolean n64, sNes, nintendo, sega, gba;
	private static boolean chkBFlag = false;

	private static int submitButton = 0;
	private static int browseButton = 0;
	
	CheckBox n64Chk, sNesChk, nintendoChk, segaChk, gbaChk;

	RadioGroup rgLayout;

	RadioButton radioLayout;

	Button btnSubmit, btnN64Browse, btnSNesBrowse, btnNintendoBrowse,
			btnSegaBrowse, btnGBABrowse, btnPathBrowse;

	EditText n64FileDest, sNesFileDest, nintendoFileDest, segaFileDest,
			gBAFileDest, deviceName;
	//EditText devicePath;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_layout);

		// Getting path out of the database for each default system directory

		m_chosenSNesDir = MainActivity.data.getSystemById(1).getSys_directory();
		m_chosenN64Dir = MainActivity.data.getSystemById(2).getSys_directory(); 
		m_chosenSegaDir = MainActivity.data.getSystemById(4).getSys_directory();
		m_chosenGBADir = MainActivity.data.getSystemById(5).getSys_directory();
		m_chosenNintendoDir = MainActivity.data.getSystemById(6).getSys_directory();
		 

		// Setting the Java variables to the Ids that were assigned
		// within the .xml file for each component.
		deviceName = (EditText) findViewById(R.id.deviceNameEdit);
		//devicePath = (EditText) findViewById(R.id.devicePathEdit);
		//btnPathBrowse = (Button) findViewById(R.id.devicePathBtn);
		rgLayout = (RadioGroup) findViewById(R.id.radiogroupLayout);
		btnSubmit = (Button) findViewById(R.id.buttonSubmit);

		btnN64Browse = (Button) findViewById(R.id.sysTableR1Btn);
		n64FileDest = (EditText) findViewById(R.id.sysTableR1Dir);
		n64Chk = (CheckBox) findViewById(R.id.sysTableR1N64Chk);

		btnSNesBrowse = (Button) findViewById(R.id.sysTableR2Btn);
		sNesFileDest = (EditText) findViewById(R.id.sysTableR2Dir);
		sNesChk = (CheckBox) findViewById(R.id.sysTableR2sNesChk);

		btnNintendoBrowse = (Button) findViewById(R.id.sysTableR3Btn);
		nintendoFileDest = (EditText) findViewById(R.id.sysTableR3Dir);
		nintendoChk = (CheckBox) findViewById(R.id.sysTableR3NintendoChk);

		btnSegaBrowse = (Button) findViewById(R.id.sysTableR4Btn);
		segaFileDest = (EditText) findViewById(R.id.sysTableR4Dir);
		segaChk = (CheckBox) findViewById(R.id.sysTableR4segaChk);

		btnGBABrowse = (Button) findViewById(R.id.sysTableR5Btn);
		gBAFileDest = (EditText) findViewById(R.id.sysTableR5Dir);
		gbaChk = (CheckBox) findViewById(R.id.sysTableR5GBAChk);

		// Necessary in order to prevent sharedPreferences from writing into the
		// preferences while being empty.
		if (submitButton > 0)
			getSettingPreferences();

	}// end on onCreate

	/**
	 * Method to handle when the submit button is clicked
	 * 
	 * @param v:		View state for submit button click
	 */
	public void onBtnSubmitClick(View v) {

		// Re-establishing the boolean state for our variables of the
		// checkboxes.
		addCheckBoxes();

		// This checks to makes sure that a user has at least one system
		// activated and the directory is not the default that is sent originally from the
		// database true		
		chkBFlag = false;
		if( n64Chk.isChecked() && !m_chosenN64Dir.equals("No Directory Set")){
			chkBFlag = true;
		}
		else if( sNesChk.isChecked() && !m_chosenSNesDir.equals("No Directory Set") ){
			chkBFlag = true;
		}
		else if( nintendoChk.isChecked() && !m_chosenNintendoDir.equals("No Directory Set")){
			chkBFlag = true;
		}
		else if( segaChk.isChecked() && !m_chosenSegaDir.equals("No Directory Set")){
			chkBFlag = true;
		}
		else if( gbaChk.isChecked() && !m_chosenGBADir.equals("No Directory Set")){
			chkBFlag = true;
		}

		if(chkBFlag == false){
			Toast.makeText(SettingActivity.this,
					"You need to select at least one checkbox and \ncorresponding directory to submit your settings",
					Toast.LENGTH_LONG).show();
		}
					
		// Restrictions met, submits the settings to shared preferences
		else {
			SharedPreferences prefs = getSharedPreferences(MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);
			if (v == btnSubmit) {

				// Getting which radioButton is selected and saving the text
				radioLayout = (RadioButton) findViewById(rgLayout
						.getCheckedRadioButtonId());
				setting_layout = radioLayout.getText().toString();

				// Checks to see if the device name is empty and catches it
				if (deviceName.getText().toString().equals("") ) {
					deviceNamePref = "";
					Toast.makeText(SettingActivity.this,
							"Must enter a device name!", Toast.LENGTH_SHORT)
							.show();
				}

				// If device name, system and its path are set
				else {
					// stores EditText device name
					
					if(submitButton == 0){
						deviceNamePref = deviceName.getText().toString();
					}
					else{
					//SharedPreferences prefs = getSharedPreferences(MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);
						deviceNamePref = prefs.getString("deviceName", "Device Name");
					
					}//end of nested else
					
					// Here is where all of the data is put into
					// the SharedPreferences and where the directory
					// is sent back into the database.
					addCheckBoxes();
					setLayoutPreferences();
					setDevicePreferences();
					setSaveDestination();
					setCheckBoxPreferences();
					
					createDir(m_chosenPathDir, "/backup");
					createDir(m_chosenPathDir, "/backup/SNES_BACKUP");
					createDir(m_chosenPathDir, "/backup/N64_BACKUP");
					createDir(m_chosenPathDir, "/backup/GBA_BACKUP");
					createDir(m_chosenPathDir, "/backup/SEGA_BACKUP");
					createDir(m_chosenPathDir, "/backup/Nintendo_BACKUP");

					MainActivity.data.updateSystemSource(m_chosenSNesDir, 1);
					MainActivity.data.updateSystemSource(m_chosenN64Dir,2);
					MainActivity.data.updateSystemSource(m_chosenSegaDir,4);
					MainActivity.data.updateSystemSource(m_chosenGBADir,5);
					MainActivity.data.updateSystemSource(m_chosenNintendoDir, 6);
					 
					submitButton++;
					
					
					//Returns to the previous activity
					checkLatestSaves();
					finish();
				
				}// end of else
		}// end of nested if (v == btnSubmit)
		}// end of else
			
	}// end of onBtnSubmitClick

	private void checkLatestSaves() {
		 List<Systems> active_systems = new ArrayList<Systems>();
		 SharedPreferences prefs = getSharedPreferences( MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE );
		 String pathToBackup = prefs.getString( "m_chosenPathDir", "") + "/backup";
		 int setting_layout = prefs.getInt("setting_layout", 0);
		 boolean gbaActive = prefs.getBoolean("GBA_CHK", false);
		 boolean snesActive = prefs.getBoolean("SNES_CHK", false);
		 boolean segaActive = prefs.getBoolean("SEGA_CHK", false);
		 boolean n64Active = prefs.getBoolean("N64_CHK", false);
		 boolean nesActive = prefs.getBoolean("NINTENDO_CHK", false);
		
		 boolean useCovers = false;
		
		 if( 0 == setting_layout ) {
		 useCovers = true;
		 }
		
		 if( gbaActive ) {
		 active_systems.add( MainActivity.data.getSystemById( 5 ) );
		 }
		
		 if( snesActive ) {
		 active_systems.add( MainActivity.data.getSystemById( 1 ) );
		 }
		
		 if( segaActive ) {
		 active_systems.add( MainActivity.data.getSystemById( 4 ) );
		 }
		
		 if( n64Active ) {
		 active_systems.add( MainActivity.data.getSystemById( 2 ) );
		 }
		
		 if( nesActive ) {
		 active_systems.add( MainActivity.data.getSystemById( 6 ) );
		 }
		 MainActivity.data.checkGamesAndSaves( active_systems, pathToBackup, useCovers );

	}

	/**
	 * Method to address when the N64 browse button is selected
	 * 
	 * @param v:		View state for N64 browse button click
	 */
	public void onBtnN64Click(View v) {
		
		// if the N64 Browse Button is selected within the View v
		if(n64Chk.isChecked()){
			
			if (v == btnN64Browse) {
	
				// Create DirectoryChooserDialog and register a callback
				DirectoryChooserDialog directoryChooserDialog = new DirectoryChooserDialog(
						SettingActivity.this,
						new DirectoryChooserDialog.ChosenDirectoryListener() {
							@Override
							public void onChosenDir(String chosenDir) {
								m_chosenN64Dir = chosenDir;
	
								// Displays which directory is selected
								Toast.makeText(SettingActivity.this,
										"Chosen directory: " + chosenDir,
										Toast.LENGTH_LONG).show();
								if(browseButton == 0)
									setDevicePath(1);
								// shows current directory in EditText field
								n64FileDest.setText(m_chosenN64Dir);
								browseButton++;
							}
	
						});
				// Load directory chooser dialog for initial 'm_chosenN64Dir'
				// directory.
				// The registered callback will be called upon final directory
				// selection.
	
				directoryChooserDialog.chooseDirectory(m_chosenN64Dir);
			}//end of (v == btnN64Browse)
		}//end of if(n64Chk.isChecked())
		else{
			Toast.makeText(SettingActivity.this, "You need to check the box first", Toast.LENGTH_SHORT).show();
		}
		
	}

	/**
	 * Method to address when the SNes browse button is selected
	 * 
	 * @param v:		View state for the SNes browse button
	 */
	public void onBtnSNesClick(View v) {
		// if the SNes Browse Button is selected within the View v
		
		if(sNesChk.isChecked()){
		
		if (v == btnSNesBrowse) {

			// Create directoryChooserDialog and register a callback
			DirectoryChooserDialog directoryChooserDialog = new DirectoryChooserDialog(
					SettingActivity.this,
					new DirectoryChooserDialog.ChosenDirectoryListener() {
						@Override
						public void onChosenDir(String chosenDir) {

							// Displays which directory is selected
							m_chosenSNesDir = chosenDir;
							Toast.makeText(SettingActivity.this,
									"Chosen directory: " + chosenDir,
									Toast.LENGTH_SHORT).show();
							if(browseButton == 0)
								setDevicePath(2);
							browseButton++;
							// Displays selected directory in EditText field
							sNesFileDest.setText(m_chosenSNesDir);
						}
					});
			// Load directory chooser dialog for initial 'm_chosenSNesDir'
			// directory.
			// The registered callback will be called upon final directory
			// selection.
			directoryChooserDialog.chooseDirectory(m_chosenSNesDir);
		}
		}else
			Toast.makeText(SettingActivity.this, "You need to check the box first", Toast.LENGTH_SHORT).show();
	}

	/**
	 * Method to address when the Nintendo Browse Button is selected
	 * 
	 * @param v:		View state for the Nintendo browse button
	 */
	public void onNintendoBtnClick(View v) {
		// if the Nintendo Browse Button is selected within the View v
		if(nintendoChk.isChecked()){
		
		if (v == btnNintendoBrowse) {

			// Create directoryChooserDialog and register a callback
			DirectoryChooserDialog directoryChooserDialog = new DirectoryChooserDialog(
					SettingActivity.this,
					new DirectoryChooserDialog.ChosenDirectoryListener() {
						@Override
						public void onChosenDir(String chosenDir) {

							// Displays which directory is selected
							m_chosenNintendoDir = chosenDir;
							Toast.makeText(SettingActivity.this,
									"Chosen directory: " + chosenDir,
									Toast.LENGTH_SHORT).show();
							if(browseButton == 0)
								setDevicePath(3);
							browseButton++;
							// Displays selected directory in the EditText field
							nintendoFileDest.setText(m_chosenNintendoDir);
						}
					});
			// Load directory chooser dialog for initial 'm_chosenNintendoDir'
			// directory.
			// The registered callback will be called upon final directory
			// selection.
			directoryChooserDialog.chooseDirectory(m_chosenNintendoDir);
		}

		}else
			Toast.makeText(SettingActivity.this, "You need to check the box first", Toast.LENGTH_SHORT).show();
	}

	/**
	 * Method to address when the Sega Browse Button is selected
	 * 
	 * @param v:		View state for the Sega browse button
	 */
	public void onBtnSegaClick(View v) {
		// if the Sega Browse Button is selected within the View v
		
		if(segaChk.isChecked()){
		if (v == btnSegaBrowse) {

			// Create directoryChooserDialog and register a callback
			DirectoryChooserDialog directoryChooserDialog = new DirectoryChooserDialog(
					SettingActivity.this,
					new DirectoryChooserDialog.ChosenDirectoryListener() {
						@Override
						public void onChosenDir(String chosenDir) {

							// Displays which directory is selected
							m_chosenSegaDir = chosenDir;
							Toast.makeText(SettingActivity.this,
									"Chosen directory: " + chosenDir,
									Toast.LENGTH_SHORT).show();
							if(browseButton == 0)
								setDevicePath(4);
							browseButton++;

							// Displays the selected directory in the EditText
							// field
							segaFileDest.setText(m_chosenSegaDir);
						}
					});

			// Load directory chooser dialog for initial 'm_chosenSegaDir'
			// directory.
			// The registered callback will be called upon final directory
			// selection.
			directoryChooserDialog.chooseDirectory(m_chosenSegaDir);
		}
		}else
			Toast.makeText(SettingActivity.this, "You need to check the box first", Toast.LENGTH_SHORT).show();
	}

	/**
	 * Method to address when the GBA Browse Button is selected
	 * 
	 * @param v:		View state for the GBA browse button
	 */
	public void onBtnGBAClick(View v) {
		// if the GBA Browse Button is selected within the View v
		
		if(gbaChk.isChecked()){
		if (v == btnGBABrowse) {

			// Create directoryChooserDialog and register a callback
			DirectoryChooserDialog directoryChooserDialog = new DirectoryChooserDialog(
					SettingActivity.this,
					new DirectoryChooserDialog.ChosenDirectoryListener() {
						@Override
						public void onChosenDir(String chosenDir) {
							m_chosenGBADir = chosenDir;

							// Displays which directory is selected
							Toast.makeText(SettingActivity.this,
									"Chosen directory: " + chosenDir,
									Toast.LENGTH_SHORT).show();
							if(browseButton == 0)
								setDevicePath(5);
							browseButton++;
							// Displays the selected directory in the EditText
							// field
							gBAFileDest.setText(m_chosenGBADir);
						}
					});

			// Load directory chooser dialog for initial 'm_chosenGBADir'
			// directory.
			// The registered callback will be called upon final directory
			// selection.
			directoryChooserDialog.chooseDirectory(m_chosenGBADir);
		}

		}else
			Toast.makeText(SettingActivity.this, "You need to check the box first", Toast.LENGTH_SHORT).show();
	}

	/**
	 * Method to set boolean values if the checkbox is selected
	 */
	public void addCheckBoxes() {
		// Has each checkBox return turn if check or false is unchecked
		// sets corresponding boolean value to what the checkBox is.
		if (sNesChk.isChecked())
			sNes = true;
		else
			sNes = false;
		if (nintendoChk.isChecked())
			nintendo = true;
		else
			nintendo = false;
		if (segaChk.isChecked())
			sega = true;
		else
			sega = false;
		if (gbaChk.isChecked())
			gba = true;
		else
			gba = false;
		if (n64Chk.isChecked())
			n64 = true;
		else
			n64 = false;
	}
	
	/**
	 * Method to set the device root path based off the 
	 * first browse destination found.
	 * @param gameID:		This is an int that was assigned to each game system
	 */
	private void setDevicePath(int systemId){
		//SharedPreferences called in order to save the path to the shared preferences
		//Additionally, there are additional variables in order to get the path w/o
		//the last folder.
		SharedPreferences prefs = getSharedPreferences(MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);
		Editor edit = prefs.edit();
		String firstDirectory, temp;
		int lastIndex;
		
		//switches between the gameId and the correct saves for each directory.
		switch(systemId){
		//Saves destination string into a variable, gets the index of the last "/"
		//in the string. Saves the string subsequence into a temp variable and then
		//replaces the temp (what we want to be rid of), and replaces it with "".
		//Finally, we save the new string into the variable for the devicePath 
		//sharedPreference and commits it.
			case 2: firstDirectory = m_chosenN64Dir;
					lastIndex = firstDirectory.lastIndexOf("/");
					temp = (String)firstDirectory.subSequence(lastIndex+1, firstDirectory.length());
					firstDirectory = firstDirectory.replace(temp, "");
					m_chosenPathDir = firstDirectory;
					edit.putString("m_chosenPathDir", m_chosenPathDir);
					edit.commit();
				break;
			case 1: firstDirectory = m_chosenSNesDir;
					lastIndex = firstDirectory.lastIndexOf("/");
					temp = (String)firstDirectory.subSequence(lastIndex+1, firstDirectory.length());
					firstDirectory = firstDirectory.replace(temp, "");
					m_chosenPathDir = firstDirectory;
					edit.putString("m_chosenPathDir", m_chosenPathDir);
					edit.commit();
				break;
			case 6: firstDirectory = m_chosenNintendoDir;
					lastIndex = firstDirectory.lastIndexOf("/");
					temp = (String)firstDirectory.subSequence(lastIndex+1, firstDirectory.length());
					firstDirectory = firstDirectory.replace(temp, "");
					m_chosenPathDir = firstDirectory;
					edit.putString("m_chosenPathDir", m_chosenPathDir);
					edit.commit();
				break;
			case 4: firstDirectory = m_chosenSegaDir;
					lastIndex = firstDirectory.lastIndexOf("/");
					temp = (String)firstDirectory.subSequence(lastIndex+1, firstDirectory.length());
					firstDirectory = firstDirectory.replace(temp, "");
					m_chosenPathDir = firstDirectory;
					edit.putString("m_chosenPathDir", m_chosenPathDir);
					edit.commit();
				break;
			case 5: firstDirectory = m_chosenGBADir;
					lastIndex = firstDirectory.lastIndexOf("/");
					temp = (String)firstDirectory.subSequence(lastIndex+1, firstDirectory.length());
					firstDirectory = firstDirectory.replace(temp, "");
					m_chosenPathDir = firstDirectory;
					edit.putString("m_chosenPathDir", m_chosenPathDir);
					edit.commit();
				break;
		}
	}

	/**
	 * Method to set which layout preference is selected
	 */
	private void setLayoutPreferences() {
		// Creates a SharedPreferences object based on the variable
		// ACCOUNT_PREFS_NAME
		SharedPreferences settings = getSharedPreferences(
				MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);

		// Retrived the id of the radioButton selected
		// and sets the layout int to the location of
		// the id. The "...0805" id references the
		// second item in the radioGroup
		int layoutInt = rgLayout.getCheckedRadioButtonId();
		if (layoutInt == 2131230805)
			layoutInt = 1;
		else
			layoutInt = 0;

		// An Editor object is essentail in order to save
		// preferences. We are saving the layoutInt into
		// the setting_layout sharedPreference we created
		Editor edit = settings.edit();
		edit.putInt("setting_layout", layoutInt);

		// Commits everything that was .put
		edit.commit();
	}

	/**
	 * Method which sets the device name to sharedPreferences
	 */
	private void setDevicePreferences() {
		// Creates a SharedPreferences object based on the variable
		// ACCOUNT_PREFS_NAME. An Editor object is essential in
		// order to save preferences.
		SharedPreferences deviceName = getSharedPreferences(
				MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);
		Editor edit = deviceName.edit();

		// Here we are assigning the device name and path into the
		// keys named deviceName, and "m_chosenPathDir for the preferences.
		edit.putString("deviceName", deviceNamePref);
		edit.putString("m_chosenPathDir", m_chosenPathDir);
		
		// Commits the deviceName
		edit.commit();
	}

	/**
	 * Method to save each game file directory
	 */
	private void setSaveDestination() {
		// Creates a SharedPreferences object based on the variable
		// ACCOUNT_PREFS_NAME. An Editor object is essentail in
		// order to save preferences.
		SharedPreferences saveLocations = getSharedPreferences(
				MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);
		Editor edit = saveLocations.edit();

		// Stores each directory with the unique value m_chosen...Dir into
		// a unique Key for only that specific data
		edit.putString("m_chosenN64Dir", m_chosenN64Dir);
		edit.putString("m_chosenSNesDir", m_chosenSNesDir);
		edit.putString("m_chosenNintendoDir", m_chosenNintendoDir);
		edit.putString("m_chosenSegaDir", m_chosenSegaDir);
		edit.putString("m_chosenGBADir", m_chosenGBADir);

		// Commits the Strings
		edit.commit();
	}

	/**
	 * Method to save checkbox preferences
	 */
	private void setCheckBoxPreferences() {
		// Creates a SharedPreferences object based on the variable
		// ACCOUNT_PREFS_NAME. An Editor object is essentail in
		// order to save preferences.
		SharedPreferences checkBoxes = getSharedPreferences(
				MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);
		Editor edit = checkBoxes.edit();

		// Stores each boolean value with its value into the Key assigned
		// to its checkbox preference location
		edit.putBoolean("N64_CHK", n64);
		edit.putBoolean("SNES_CHK", sNes);
		edit.putBoolean("NINTENDO_CHK", nintendoChk.isChecked());
		edit.putBoolean("SEGA_CHK", segaChk.isChecked());
		edit.putBoolean("GBA_CHK", gbaChk.isChecked());

		// Commits each boolean value
		edit.commit();
	}

	/**
	 * Method returns all of the sharedPreferences from this Activity
	 */
	public void getSettingPreferences() {
		// Creates a SharedPreferences object based on the variable
		// ACCOUNT_PREFS_NAME.
		SharedPreferences prefs = this.getSharedPreferences(
				MainActivity.ACCOUNT_PREFS_NAME, Context.MODE_PRIVATE);

		// This is reading in all of the text information from the Keys
		// that previous data had been stored in. The second strings are
		// default values in case the Key is empty
		deviceName.setText(prefs.getString("deviceName", "Device Name"));
		m_chosenPathDir = prefs.getString("m_chosenPathDir", "Device Path");
		n64FileDest.setText(prefs.getString("m_chosenN64Dir", "N64 Directory"));
		sNesFileDest.setText(prefs.getString("m_chosenSNesDir",
				"SNes Directory"));
		nintendoFileDest.setText(prefs.getString("m_chosenNintendoDir",
				"Nintendo Directory"));
		segaFileDest.setText(prefs.getString("m_chosenSegaDir",
				"Sega Directory"));
		gBAFileDest.setText(prefs.getString("m_chosenGBADir", "GBA Directory"));

		// Gets the in value of what was saved to the preferences setting_layout
		// It then gets the radioButton at that location, and sets it to true
		int checkBoxLocation = prefs.getInt("setting_layout", 0);
		radioLayout = (RadioButton) rgLayout.getChildAt(checkBoxLocation);
		radioLayout.setChecked(true);

		// Gets the boolean value from the system checkBox, true = checked.
		// Then, has the checkbox get set to the value that was returned.
		n64 = prefs.getBoolean("N64_CHK", false);
		n64Chk.setChecked(n64);
		sNes = prefs.getBoolean("SNES_CHK", false);
		sNesChk.setChecked(sNes);
		nintendo = prefs.getBoolean("NINTENDO_CHK", false);
		nintendoChk.setChecked(nintendo);
		sega = prefs.getBoolean("SEGA_CHK", false);
		segaChk.setChecked(sega);
		gba = prefs.getBoolean("GBA_CHK", false);
		gbaChk.setChecked(gba);
	}
	
	/**
	 * The createDir() method creates a backup directory in the users android
	 * devices root directory. The directory ROM/backup will be created on first
	 * start of the MainActivity.
	 * 
	 * @param dirPath
	 *            the dirPath, not null
	 * @return true if the file is created, false if the file exists or has not
	 *         been created
	 */
	public boolean createDir(String basePath, String dirPath) {
		File dirNew = new File(basePath, dirPath);
		if (!dirNew.exists()) {
			return dirNew.mkdirs();
		}
		return false;
	}
}// end of class